package _1Cookie;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/ShowCookieServlet")
public class ShowCookieServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter pw = resp.getWriter();
		
		//从请求对象中取出Cookie
		Cookie[] cookies = req.getCookies();
		
		if(cookies!=null) {
			for(int i=0;i<cookies.length;i++) {
				Cookie c = cookies[i];
				pw.print(c.getName()+":"+c.getValue());
			}
		}
		else {
			pw.print("没有收到Cookie信息");
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}